<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Delete Campaign Handler (/admin/handler.campaign_delete.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require dirname(__FILE__) . "/../config.php";

// Start session if not already started
session_start();

if(!isset($_SESSION['login_t'])) {

	/* If we're not logged in, take us to the login page and
	ensure the rest of the code isn't run */

	header("Location: /admin/login.php");
	exit;
}

$pagetitle = "Delete Campaign (Admin)";
$pageheading = "<a href=\"/admin/index.php\">" . SITE_NAME . " Administration</a> &gt; Delete Campaign";
$page_content = "<hr />\n";

if(!isset($_GET['cid']) || !is_numeric($_GET['cid'])) {
	// No campaign to delete
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No campaign was selected for deletion. To select a campaign for deletion, use its respective \"Delete\" icon on the List Campaigns page.</div>\n";
} else if($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_GET['cid'] . "'") == false) {
	// Non-existent campaign
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The campaign selected does not exist. It may have already been deleted.</div>\n";
} else {
	// Get campaign info
	$campaign = $db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_GET['cid'] . "'");
	$offers = $db->get_results("SELECT * FROM `" . DB_TBL_PREFIX . "offers` WHERE `campaign_id` = '" . $_GET['cid'] . "'");
	if(!isset($_GET['confirm']) || $_GET['confirm'] != 1) {
		// Confirm deletion
		$page_content .= "<p>Are you sure you want to delete the '" . $campaign->campaign_name . "' campaign? This will also delete all offers associated with the campaign.</p>\n";
		$page_content .= "<p><a href=\"handler.campaign_delete.php?cid=" . $_GET['cid'] . "&amp;confirm=1\">Yes, delete it.</a></p>\n";
		$page_content .= "<p><a href=\"list_campaigns.php\">No, do not delete it.</a></p>\n";
	} else {
		// Campaign deletion confirmed, perform delete
		$delcampaign = $db->query("DELETE FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_id` = '" . $_GET['cid'] . "'");
		
		$page_content .= "<div id=\"success-box\">The '" . $campaign->campaign_name . "' campaign was deleted successfully.";
		
		if($offers) {
			foreach( $offers as $offer ) {
				$delcampaign = $db->query("DELETE FROM `" . DB_TBL_PREFIX . "offers` WHERE `campaign_id` = '" . $offer->offer_id . "'");
				$fname = CACHE_DIR . "/" . $campaign->campaign_id . "_" . $offer->offer_id . ".png";
				if(file_exists($fname)) unlink($fname);
			}
		$page_content .= " In addition, all offers associated with the campaign were removed.";
		}
		$page_content .= "</div>\n";
	}
}
$page_content .= "<p class=\"centered\"><a href=\"/admin/list_campaigns.php\">Back</a> to Campaign List</p>\n";
$page_content .= "<hr />\n";
$page_content .= "<p><a href=\"/admin/index.php\">Back</a> to " . SITE_NAME . " Administration</p>\n";

// Require template
require TEMPLATE_DIR . "/index.php";
?>